package com.alibaba.citrus.cr.mn.order.center.facade.function.check;

import com.alibaba.citrus.cr.common.enums.ordercenter.*;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.JsonUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.epoch.app.mnordercenter.model.dto.DictionaryResponse;
import com.epoch.app.mnordercenter.model.dto.ReturnOrderAtomCreateRequest;
import com.epoch.app.mnordercenter.model.dto.SalesOrderAtomCreateLineRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCreateResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName ReturnOrderBizCreateParamsCheck
 * @description: 退货订单商业创建入参校验
 * @date 2023/05/11 16:35
 * @version: 1.0
 */
@Service("returnOrderBizCreateParamsCheck")
public class ReturnOrderBizCreateParamsCheck extends AbstractOrderParamsCheckService<ReturnOrderAtomCreateRequest, OrderAtomCreateResponse> {

    @Resource
    private BaseDataServiceAdapter baseDataServiceAdapter;

    private static final List<String> SALES_ORDER_TYPE_GROUP_LIST = Arrays.asList(ReturnOrderTypeEnum.RE_ORDER_GROUP.getCode(),ReturnOrderTypeEnum.RE_ORDER_FREE_GROUP.getCode());

    @Override
    public void paramsNotBlankCheck(List<ReturnOrderAtomCreateRequest> requestList, List<OrderAtomCreateResponse> responseList) {
        AssertUtils.isTrue(CollectionUtils.isEmpty(requestList), "OC-01-001-01-15-001", "单据列表");
        AssertUtils.isTrue(requestList.size() > 100, "OC-01-001-01-15-004", "单据列表", requestList.size(), "100");
        requestList.forEach(t -> {
            AssertUtils.isTrue(StringUtils.isBlank(t.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getSourceOrderNo()), "OC-01-001-01-15-001", "来源单号");
            AssertUtils.isTrue(StringUtils.isBlank(t.getSalesOrderType()), "OC-01-001-01-15-001", "订单类型编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getCustomerCode()), "OC-01-001-01-15-001", "客户售达方编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getShipToPartyCode()), "OC-01-001-01-15-001", "客户送达方编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getBusinessChannel()), "OC-01-001-01-15-001", "业务渠道编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getSalesOrganizationCode()), "OC-01-001-01-15-001", "销售组织编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getProductGroupCode()), "OC-01-001-01-15-001", "产品组编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getExpectedDeliveryDate()), "OC-01-001-01-15-001", "期望交货日期");
            AssertUtils.isTrue(StringUtils.isBlank(t.getDeliveryMethodCode()), "OC-01-001-01-15-001", "配送方式编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getCreatorCode()), "OC-01-001-01-15-001", "创建人工号");
            if (OrderCategoryEnum.GROUP.getCode().equals(t.getOrderCategoryCode()) || OrderCategoryEnum.COMPANY.getCode().equals(t.getOrderCategoryCode())){
                AssertUtils.isTrue(StringUtils.isBlank(t.getOrderChannelCode()), "OC-01-001-01-15-001", "订单渠道编码");
            }
            AssertUtils.isTrue(StringUtils.isBlank(t.getOrderReasonCode()), "OC-01-001-01-15-001", "订单原因编码");
            AssertUtils.isTrue(CollectionUtils.isEmpty(t.getOrderDetails()), "OC-01-001-01-15-001", "单据明细");
            AssertUtils.isTrue(t.getOrderDetails().size() > 100, "OC-01-001-01-15-004", "单据明细", t.getOrderDetails().size(), "100");

            // 校验单据明细
            t.getOrderDetails().forEach(detail -> {
                AssertUtils.isTrue(StringUtils.isBlank(detail.getSourceLineNum()), "OC-01-001-01-15-001", "来源单行号");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getProductCode()), "OC-01-001-01-15-001", "物料编码");
                AssertUtils.isTrue(StringUtils.isBlank(detail.getSalesUnitCode()), "OC-01-001-01-15-001", "销售单位编码");

                if (OrderCategoryEnum.GROUP.getCode().equals(t.getOrderCategoryCode())){
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getProductQuantity()), "OC-01-001-01-15-001", "退货数量");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getProductionDate()), "OC-01-001-01-15-001", "生产日期");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getInvoiceNo()), "OC-01-001-01-15-001", "发票号");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getInvoiceLineNo()), "OC-01-001-01-15-001", "发票行号");
                }else {
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getProductQuantity()), "OC-01-001-01-15-001", "销售数量");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getManualTotalAmount()), "OC-01-001-01-15-001", "手工总金额");
                    AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getManualTotalAmount())) < 0, "OC-01-001-01-15-009", "手工总金额", "最大2147483647");
                }

                if (StringUtils.isBlank(detail.getShippingWarehouseCode())) {
                    if (StringUtils.isBlank(detail.getDeliveryFactoryCode()) && StringUtils.isBlank(detail.getStockCode())) {
                        throw new FunctionException("OC-01-001-01-15-010", "逻辑仓库编码、交货工厂编码、库存地点编码");
                    }
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getDeliveryFactoryCode()), "OC-01-001-01-15-001", "交货工厂编码");
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getStockCode()), "OC-01-001-01-15-001", "库存地点编码");
                }

                if (OrderCategoryEnum.GROUP.getCode().equals(t.getOrderCategoryCode())){
                    if (StringUtils.isNotBlank(detail.getTxWarehouseCode())
                            || StringUtils.isNotBlank(detail.getTxFactoryCode())
                            || StringUtils.isNotBlank(detail.getTxStockCode())){
                        //二选一必填
                        if (StringUtils.isBlank(detail.getTxWarehouseCode())) {
                            if (StringUtils.isBlank(detail.getTxFactoryCode()) && StringUtils.isBlank(detail.getTxStockCode())) {
                                throw new FunctionException("OC-01-001-01-15-010", "发货仓编码、发货工厂编码、发货库存地点编码");
                            }
                            AssertUtils.isTrue(StringUtils.isBlank(detail.getTxFactoryCode()), "OC-01-001-01-15-001", "发货工厂编码");
                            AssertUtils.isTrue(StringUtils.isBlank(detail.getTxStockCode()), "OC-01-001-01-15-001", "发货库存地点编码");
                        }
                        //校验发货仓编码
//                        List<String> txWarehouseCodes = t.getOrderDetails().stream()
//                                .map(SalesOrderAtomCreateLineRequest::getTxWarehouseCode)
//                                .filter(StringUtils::isNotBlank)
//                                .distinct().collect(Collectors.toList());
//                        if (CollectionUtils.isNotEmpty(txWarehouseCodes) && txWarehouseCodes.size() > 1) {
//                            throw new FunctionException("OC-01-001-01-15-003", t.getSourceOrderNo(), StringUtils.join(txWarehouseCodes, ","));
//                        } else {
//                            List<String> tmpCodes = t.getOrderDetails().stream()
//                                    .filter(p -> StringUtils.isNotBlank(p.getTxFactoryCode()) && StringUtils.isNotBlank(p.getTxStockCode()))
//                                    .map(p -> String.format("%s_%s", p.getTxFactoryCode(), p.getTxStockCode()))
//                                    .distinct().collect(Collectors.toList());
//                            if (CollectionUtils.isNotEmpty(tmpCodes) && tmpCodes.size() > 1) {
//                                throw new FunctionException("OC-01-001-01-15-003", t.getSourceOrderNo(), StringUtils.join(tmpCodes, ","));
//                            }
//                        }
                    }
                }
            });

            // 校验单据明细行号是否重复
            List<String> sameSourceLines = t.getOrderDetails().stream()
                    .map(SalesOrderAtomCreateLineRequest::getSourceLineNum)
                    .collect(Collectors.toMap(p -> p, p -> 1, Integer::sum))
                    .entrySet().stream().filter(entry -> entry.getValue() > 1)
                    .map(Map.Entry::getKey).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(sameSourceLines)) {
                throw new FunctionException("OC-01-001-01-15-002", "来源单行号", StringUtils.join(sameSourceLines, ","));
            }
        });

        // 校验来源单号是否重复
        List<String> sameSourceOrderNos = requestList.stream()
                .map(ReturnOrderAtomCreateRequest::getSourceOrderNo)
                .collect(Collectors.toMap(t -> t, t -> 1, Integer::sum))
                .entrySet().stream().filter(entry -> entry.getValue() > 1)
                .map(Map.Entry::getKey).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(sameSourceOrderNos)) {
            throw new FunctionException("OC-01-001-01-15-002", "来源单号", StringUtils.join(sameSourceOrderNos, ","));
        }
    }

    @Override
    public void paramsLengthCheck(List<ReturnOrderAtomCreateRequest> requestList, List<OrderAtomCreateResponse> responseList) {
        requestList.forEach(t -> {
            AssertUtils.isNumber(t.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");
            AssertUtils.lengthCheck(t.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
            AssertUtils.lengthCheck(t.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
            AssertUtils.lengthCheck(t.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
            AssertUtils.lengthCheck(t.getSourceOrderNo(), 50, true, "OC-01-001-01-15-004", "来源单号");
            AssertUtils.lengthCheck(t.getOriginalOrderNo(), 50, false, "OC-01-001-01-15-004", "原销售订单单号");
            AssertUtils.lengthCheck(t.getProductGroupCode(), 50, true, "OC-01-001-01-15-004", "产品组编码");
            AssertUtils.lengthCheck(t.getBusinessChannel(), 50, true, "OC-01-001-01-15-004", "业务渠道编码");
            AssertUtils.lengthCheck(t.getSalesOrganizationCode(), 50, true, "OC-01-001-01-15-004", "销售组织编码");
            AssertUtils.lengthCheck(t.getCustomerCode(), 50, true, "OC-01-001-01-15-004", "客户售达方编码");
            AssertUtils.lengthCheck(t.getShipToPartyCode(), 50, true, "OC-01-001-01-15-004", "客户送达方编码");
            AssertUtils.lengthCheck(t.getSalesChannelCode(), 50, false, "OC-01-001-01-15-004", "销售渠道编码");
            AssertUtils.lengthCheck(t.getSalesDepartmentCode(), 50, false, "OC-01-001-01-15-004", "销售部门编码");
            AssertUtils.lengthCheck(t.getSalesOrderType(), 10, true, "OC-01-001-01-15-004", "订单类型编码");
            AssertUtils.lengthCheck(t.getDeliveryMethodCode(), 50, true, "OC-01-001-01-15-004", "配送方式编码");
            AssertUtils.lengthCheck(t.getExpectedDeliveryDate(), 10, true, "OC-01-001-01-15-004", "期望交货日期");
            AssertUtils.isTrue(!DateUtils.isDateFormat(t.getExpectedDeliveryDate()), "OC-00-001-01-15-001", "期望交货日期");
            //AssertUtils.isTrue(DateUtils.formatDate(new Date()).compareTo(t.getExpectedDeliveryDate()) > 0, "OC-01-001-01-16-021");
            AssertUtils.lengthCheck(t.getCreatorCode(), 50, true, "OC-01-001-01-15-004", "创建人工号");
            AssertUtils.lengthCheck(t.getCustomerPurchaseOrderNo(), 50, false, "OC-01-001-01-15-004", "客户采购订单号");
            AssertUtils.lengthCheck(t.getCustomerPurchaseDate(), 10, false, "OC-01-001-01-15-004", "客户采购订单日期");
            if (StringUtils.isNotBlank(t.getCustomerPurchaseDate())) {
                AssertUtils.isTrue(!DateUtils.isDateFormat(t.getCustomerPurchaseDate()), "OC-00-001-01-15-001", "客户采购订单日期");
            }
            AssertUtils.lengthCheck(t.getNote(), 255, false, "OC-01-001-01-15-004", "订单头备注");
            AssertUtils.lengthCheck(t.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");
            AssertUtils.lengthCheck(t.getOrderReasonCode(), 32, true, "OC-01-001-01-15-004", "订单原因编码");
            AssertUtils.lengthCheck(t.getCustomerGrade(), 50, false, "OC-01-001-01-15-004", "客户等级");
            AssertUtils.lengthCheck(t.getCustomerGroup(), 50, false, "OC-01-001-01-15-004", "客户组编码");

            if (OrderCategoryEnum.GROUP.getCode().equals(t.getOrderCategoryCode()) || OrderCategoryEnum.COMPANY.getCode().equals(t.getOrderCategoryCode())){
                AssertUtils.lengthCheck(t.getOrderChannelCode(), 50, true, "OC-01-001-01-15-004", "订单渠道编码");
                AssertUtils.lengthCheck(t.getSalesChannelName(), 50, false, "OC-01-001-01-15-004", "销售渠道名称");
                AssertUtils.lengthCheck(t.getSalesDepartmentName(), 50, false, "OC-01-001-01-15-004", "销售部门名称");
                AssertUtils.lengthCheck(t.getCostCenterCode(), 50, false, "OC-01-001-01-15-004", "成本中心");
                if (CollectionUtils.isNotEmpty(t.getOrderBizTag())) {
                    AssertUtils.isTrue(t.getOrderBizTag().size() > 10, "OC-01-001-01-15-004", "订单标签集合", t.getOrderBizTag().size(), "10");
                    for (String orderTag : t.getOrderBizTag()) {
                        AssertUtils.lengthCheck(orderTag, 50, "OC-01-001-01-15-004", "订单标签");
                    }
                }
                AssertUtils.lengthCheck(t.getFulfillmentTypeCode(), 50, false, "OC-01-001-01-15-004", "履约方式");
                AssertUtils.lengthCheck(t.getAutoFulfillment(), 5, false, "OC-01-001-01-15-004", "是否自动创建履约单");
                AssertUtils.lengthCheck(t.getFulfillmentPriority(), 10, false, "OC-01-001-01-15-004", "履约优先级");
            }

            t.getOrderDetails().forEach(detail -> {
                AssertUtils.lengthCheck(detail.getSourceLineNum(), 10, true, "OC-01-001-01-15-004", "来源单行号");
                AssertUtils.lengthCheck(detail.getProductCode(), 50, true, "OC-01-001-01-15-004", "物料编码");
                AssertUtils.lengthCheck(detail.getSalesUnitCode(), 50, true, "OC-01-001-01-15-004", "销售单位编码");
                AssertUtils.lengthCheck(detail.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");
                AssertUtils.lengthCheck(detail.getNote(), 255, false, "OC-01-001-01-15-004", "订单行备注");
                if (OrderCategoryEnum.GROUP.getCode().equals(t.getOrderCategoryCode())){
                    AssertUtils.lengthCheck(detail.getTxWarehouseCode(), 50, false, "OC-01-001-01-15-004", "发货仓编码");
                    AssertUtils.lengthCheck(detail.getTxFactoryCode(), 50, false, "OC-01-001-01-15-004", "发货工厂编码");
                    AssertUtils.lengthCheck(detail.getTxStockCode(), 50, false, "OC-01-001-01-15-004", "发货库存地点编码");
                    AssertUtils.isWholeNumber(detail.getProductQuantity(), true, "OC-01-001-01-15-009", "退货数量", "退货数量应为正整数");
                    AssertUtils.lengthCheck(detail.getProductQuantity(), 10, true, "OC-01-001-01-15-004", "退货数量");
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getProductQuantity())) >= 0, "OC-01-001-01-15-009", "退货数量", "退货数量须为正整数");
                    AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getProductQuantity())) < 0, "OC-01-001-01-15-009", "退货数量", "最大2147483647");
                    AssertUtils.timeFormatCheck(detail.getProductionDate(), null, "生产日期", null, AssertUtils.TYPE_DATE);
                    AssertUtils.lengthCheck(detail.getInvoiceNo(), 50, false, "OC-01-001-01-15-004", "发票号");
                    AssertUtils.lengthCheck(detail.getInvoiceLineNo(), 50, false, "OC-01-001-01-15-004", "发票行号");

//                    //校验发货仓库编码
//                    if (StringUtils.isNotBlank(detail.getTxWarehouseCode())
//                            || StringUtils.isNotBlank(detail.getTxFactoryCode())
//                            || StringUtils.isNotBlank(detail.getTxStockCode())){
//                        //校验发货仓编码
//                        List<String> txWarehouseCodes = t.getOrderDetails().stream()
//                                .map(SalesOrderAtomCreateLineRequest::getTxWarehouseCode)
//                                .filter(StringUtils::isNotBlank)
//                                .distinct().collect(Collectors.toList());
//                        if (CollectionUtils.isNotEmpty(txWarehouseCodes) && txWarehouseCodes.size() > 1) {
//                            throw new FunctionException("OC-01-001-01-15-003", t.getSourceOrderNo(), StringUtils.join(txWarehouseCodes, ","));
//                        } else {
//                            List<String> tmpCodes = t.getOrderDetails().stream()
//                                    .filter(p -> StringUtils.isNotBlank(p.getTxFactoryCode()) && StringUtils.isNotBlank(p.getTxStockCode()))
//                                    .map(p -> String.format("%s_%s", p.getTxFactoryCode(), p.getTxStockCode()))
//                                    .distinct().collect(Collectors.toList());
//                            if (CollectionUtils.isNotEmpty(tmpCodes) && tmpCodes.size() > 1) {
//                                throw new FunctionException("OC-01-001-01-15-003", t.getSourceOrderNo(), StringUtils.join(tmpCodes, ","));
//                            }
//                        }
//                    }
                    AssertUtils.lengthCheck(detail.getShippingWarehouseCode(), 50, false, "OC-01-001-01-15-004", "收货仓库编码");
                    AssertUtils.lengthCheck(detail.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "收货工厂编码");
                    AssertUtils.lengthCheck(detail.getStockCode(), 50, false, "OC-01-001-01-15-004", "收货库存地点编码");
                }else {
                    AssertUtils.lengthCheck(detail.getShippingWarehouseCode(), 50, false, "OC-01-001-01-15-004", "逻辑仓库编码");
                    AssertUtils.lengthCheck(detail.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "交货工厂编码");
                    AssertUtils.lengthCheck(detail.getStockCode(), 50, false, "OC-01-001-01-15-004", "库存地点编码");
                    AssertUtils.isWholeNumber(detail.getProductQuantity(), true, "OC-01-001-01-15-009", "退货数量", "退货数量应为正整数");
                    AssertUtils.lengthCheck(detail.getProductQuantity(), 10, true, "OC-01-001-01-15-004", "退货数量");
                    AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getProductQuantity())) >= 0, "OC-01-001-01-15-009", "退货数量", "退货数量须为正整数");
                    AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getProductQuantity())) < 0, "OC-01-001-01-15-009", "退货数量", "最大2147483647");
                    AssertUtils.isNumber(detail.getManualTotalAmount(), true, "OC-00-001-01-15-001", "手工总金额");
                    AssertUtils.lengthCheck(detail.getManualTotalAmount(), 10, true, "OC-01-001-01-15-004", "手工总金额");
                    AssertUtils.moneyCheck(detail.getManualTotalAmount(), true, "OC-01-001-01-15-005", "手工总金额");
                    AssertUtils.lengthCheck(detail.getProductionDate(), 10, false, "OC-01-001-01-15-004", "生产日期");
                    if(StringUtils.isNotBlank(detail.getProductionDate())) {
                        AssertUtils.isTrue(!DateUtils.isDateFormat(detail.getProductionDate()), "OC-00-001-01-15-001", "生产日期");
                    }
                }

                // 校验逻辑仓库编码
//                List<String> shippingWarehouseCodes = t.getOrderDetails().stream()
//                        .map(SalesOrderAtomCreateLineRequest::getShippingWarehouseCode)
//                        .filter(StringUtils::isNotBlank)
//                        .distinct().collect(Collectors.toList());
//                if (CollectionUtils.isNotEmpty(shippingWarehouseCodes) && shippingWarehouseCodes.size() > 1) {
//                    throw new FunctionException("OC-01-001-01-15-003", t.getSourceOrderNo(), StringUtils.join(shippingWarehouseCodes, ","));
//                } else {
//                    List<String> tmpCodes = t.getOrderDetails().stream()
//                            .filter(p -> StringUtils.isNotBlank(p.getDeliveryFactoryCode()) && StringUtils.isNotBlank(p.getStockCode()))
//                            .map(p -> String.format("%s_%s", p.getDeliveryFactoryCode(), p.getStockCode()))
//                            .distinct().collect(Collectors.toList());
//                    if (CollectionUtils.isNotEmpty(tmpCodes) && tmpCodes.size() > 1) {
//                        throw new FunctionException("OC-01-001-01-15-003", t.getSourceOrderNo(), StringUtils.join(tmpCodes, ","));
//                    }
//                }
            });
        });
    }

    @Override
    public void paramsEnumDictCheck(List<ReturnOrderAtomCreateRequest> requestList, List<OrderAtomCreateResponse> responseList) {

        // 系统来源
        List<DictionaryResponse> fromSysResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.FROM_SYS.getCode());
        Map<String, DictionaryResponse> fromSysMap = fromSysResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        //业态
        List<DictionaryResponse> businessTypeResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        //业务单元
        List<DictionaryResponse> businessUnitCodeList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_UNIT_CODE.getCode());
        Map<String, DictionaryResponse> businessUnitCodeMap = businessUnitCodeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        //履约方式
        List<DictionaryResponse> fulfillmentTypeList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.GROUP_RETURN_FULFILLMENT_TYPE.getCode());
        Map<String, DictionaryResponse> fulfillmentTypeMap = fulfillmentTypeList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));

        requestList.forEach(t -> {
            if (fromSysMap.get(t.getFromSys()) == null) {
                throw new FunctionException("OC-01-001-01-15-006", "来源系统编码");
            }
            DictionaryResponse dictionaryResponse = businessTypeMap.get(t.getBusinessType());
            if (dictionaryResponse == null) {
                throw new FunctionException("OC-01-001-01-15-006", "业态编码");
            }
            DictionaryResponse businessUnitCodeResponse = businessUnitCodeMap.get(t.getBusinessUnitCode());
            if (businessUnitCodeResponse == null) {
                throw new FunctionException("OC-01-001-01-15-006", "业务单元编码");
            }

            t.setBusinessTypeName(dictionaryResponse.getName());
            t.setBusinessUnitName(businessUnitCodeResponse.getName());
            boolean isGroup = t.getOrderCategoryCode().equals(OrderCategoryEnum.GROUP.getCode());
            if (isGroup){
                if (!SALES_ORDER_TYPE_GROUP_LIST.contains(t.getSalesOrderType())){
                    throw new FunctionException("OC-01-001-01-15-006", "订单类型编码");
                }
                if (ReturnOrderTypeEnum.RE_ORDER_FREE_GROUP.getCode().equals(t.getSalesOrderType())){
                    AssertUtils.isTrue(StringUtils.isBlank(t.getCostCenterCode()),"OC-01-001-01-15-001","成本中心");
                }
                ReturnOrderTypeEnum salesOrderTypeEnum = ReturnOrderTypeEnum.of(t.getSalesOrderType());
                t.setSalesOrderTypeName(salesOrderTypeEnum.getDesc());
                // 校验订单渠道编码
                if (StringUtils.isNotBlank(t.getOrderChannelCode())) {
                    OrderChannelEnum orderChannelEnum = OrderChannelEnum.of(t.getOrderChannelCode());
                    Optional.ofNullable(orderChannelEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单渠道编码"));
                    t.setOrderChannelName(orderChannelEnum.getDesc());
                }
                //校验履约方式
                if (StringUtils.isNotBlank(t.getFulfillmentTypeCode()) && Objects.isNull(fulfillmentTypeMap.get(t.getFulfillmentTypeCode()))){
                    throw new FunctionException("OC-01-001-01-15-006", "履约方式编码");
                }
                //校验履约优先级
                if (StringUtils.isNotBlank(t.getFulfillmentPriority())){
                    FulfillmentPriorityEnum fulfillmentPriorityEnum = FulfillmentPriorityEnum.of(t.getFulfillmentPriority());
                    Optional.ofNullable(fulfillmentPriorityEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "履约优先级"));
                }
            }else {
                ReturnOrderTypeEnum salesOrderTypeEnum = ReturnOrderTypeEnum.companyOrderOf(t.getSalesOrderType());
                Optional.ofNullable(salesOrderTypeEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单类型编码"));
                t.setSalesOrderTypeName(salesOrderTypeEnum.getDesc());
            }

            SalesOrderDeliveryMethodEnum salesOrderDeliveryMethodEnum = SalesOrderDeliveryMethodEnum.of(t.getDeliveryMethodCode());
            Optional.ofNullable(salesOrderDeliveryMethodEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "配送方式编码"));
            t.setDeliveryMethodName(salesOrderDeliveryMethodEnum.getDesc());

            if (StringUtils.isNotBlank(t.getOrderReasonCode())){
                SalesOrderReasonEnum salesOrderReasonEnum = SalesOrderReasonEnum.of(t.getOrderReasonCode());
                Optional.ofNullable(salesOrderReasonEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单原因编码"));
            }

            if (StringUtils.isNotBlank(t.getCustomerGroup())){
                CustomerGroupEnum customerGroupEnum = CustomerGroupEnum.of(t.getCustomerGroup());
                if (Objects.nonNull(customerGroupEnum)) {
                    // 设置客户分组名称
                    t.setCustomerGroupName(customerGroupEnum.getDesc());
                }
            }

            if (CollectionUtils.isNotEmpty(t.getOrderBizTag())){
                Set<String> orderBizTagSet = new HashSet<>();
                t.getOrderBizTag().stream().peek(a->{
                            OrderBizTagEnum orderBizTagEnum = OrderBizTagEnum.of(a);
                            if (Objects.isNull(orderBizTagEnum)) throw new FunctionException("OC-01-001-01-15-006","订单标签");
                        })
                        .forEach(b->{
                            if (orderBizTagSet.contains(b)){
                                throw new FunctionException("OC-01-001-01-15-002","订单标签",b);
                            }else {
                                orderBizTagSet.add(b);
                            }
                });
            }

            // 校验扩展字段是否为JSON格式
            AssertUtils.isTrue(!JsonUtils.isValidJson(t.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");

            for (SalesOrderAtomCreateLineRequest p : t.getOrderDetails()) {
                StockUnitEnum salesUnit = StockUnitEnum.of(p.getSalesUnitCode());
                Optional.ofNullable(salesUnit).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "销售单位编码"));
                p.setSalesUnitName(salesUnit.getDesc());

                // 校验扩展字段是否为JSON格式
                AssertUtils.isTrue(!JsonUtils.isValidJson(p.getBizFeatures()), "OC-00-001-01-15-001", "扩展字段");
            }
        });
    }
}
